package com.cloudkinto.service.consumer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cloudkinto.common.RabbitQueueConstant;
import com.cloudkinto.entity.PlatformStoreDo;
import com.cloudkinto.service.consumer.vo.markShip.Qoo10MarkShipConsumerReq;
import com.cloudkinto.service.consumer.vo.orderSyncRecord.PlatformOrderSyncRecordReq;
import com.cloudkinto.service.consumer.vo.qoo10.Qoo10ClaimInfoConsumerReq;
import com.cloudkinto.service.consumer.vo.qoo10.Qoo10ConsumerReq;
import com.cloudkinto.service.consumer.vo.rakuten.ItemUpdateConsumerReq;
import com.cloudkinto.service.consumer.vo.rakuten.RakutenOrderUpdateConReq;
import com.cloudkinto.service.platform.qoo10.PlatformOrderQoo10Service;
import com.cloudkinto.service.platform.qoo10.vo.SetCheckYnConsumerReq;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component
@RequiredArgsConstructor
public class PlatformOrderQoo10Consumer {
    private final RedisTemplate redisTemplate;
    private final PlatformOrderQoo10Service platformOrderQoo10Service;
    private final RabbitTemplate rabbitTemplate;

    /**
     * 同步商品 获取itemCode
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.GetPlatformProduct_Qoo10))
    public void getAllGoodsInfo(String msg) {
        ItemUpdateConsumerReq req = JSONObject.parseObject(msg, ItemUpdateConsumerReq.class);
        try {
            platformOrderQoo10Service.getAllGoodsInfo(req.getStoreDo());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 同步商品 获取itemCode
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.GetPlatformProduct_Qoo10_ParentSku))
    public void getGoodsInventoryInfo(String msg) {
        ItemUpdateConsumerReq req = JSONObject.parseObject(msg, ItemUpdateConsumerReq.class);
        try {
            platformOrderQoo10Service.getGoodsInventoryInfo(req.getStoreDo(), req.getParentSku(), null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    /**
     * 同步订单表历史
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformStore_Sync_History_Qoo10))
    public void syncOrderQoo10History(String msg) {
        Qoo10ConsumerReq req = JSONObject.parseObject(msg, Qoo10ConsumerReq.class);
        String s = platformOrderQoo10Service.updateQoo10OrdersHistory(req);
        if (StringUtils.isNotBlank(s)) {
            log.error(s);
        }
    }



    /**
     * 同步订单表
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformStore_Sync_Qoo10))
    public void syncOrderQoo10(String msg) {
        Qoo10ConsumerReq req = JSONObject.parseObject(msg, Qoo10ConsumerReq.class);
        String s = "";
        try {
            s = platformOrderQoo10Service.updateQoo10Orders(req);
        } catch (Exception e) {
            s = e.getMessage();
        }
        if (StringUtils.isNotBlank(s)) {
            rabbitTemplate.convertAndSend(RabbitQueueConstant.PlatformOrder_SyncRecord_Update, JSON.toJSONString(new PlatformOrderSyncRecordReq(req.getSyncId(), 3, new Date(), null, s, null)));
        }
    }



    /**
     * 同步取消订单表
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformStore_Sync_Qoo10_Claim))
    public void syncOrderClaimQoo10(String msg) {
        Qoo10ClaimInfoConsumerReq req = JSONObject.parseObject(msg, Qoo10ClaimInfoConsumerReq.class);
        String s = null;
        try {
           s = platformOrderQoo10Service.getClaimInfo(req);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (StringUtils.isNotBlank(s)) {
            log.error(s);
        }
    }


    /**
     * 更新订单表
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformStore_Qoo10_Update))
    public void syncOrderQoo10Update(String msg) {
        RakutenOrderUpdateConReq req = JSONObject.parseObject(msg, RakutenOrderUpdateConReq.class);
        try {
            platformOrderQoo10Service.consumerUpdate(req);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 标发
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformStore_Qoo10_MarkShip))
    public void markShip(String msg) {
        Qoo10MarkShipConsumerReq req = JSONObject.parseObject(msg, Qoo10MarkShipConsumerReq.class);
        try {
            platformOrderQoo10Service.makeShip(req, req.getUserId());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 给qoo10订单 自动填発送予定日
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformStore_Qoo10_SetSellerCheckYN))
    public void setSellerCheckYN_V2(String msg) {
        SetCheckYnConsumerReq req = JSONObject.parseObject(msg, SetCheckYnConsumerReq.class);
        String s = null;
        try {
            s = platformOrderQoo10Service.setSellerCheckYN_V2(req.getSetSellerCheckYNReqList(), req.getStoreDo());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (StringUtils.isNotBlank(s)) {
            log.error(s);
        }
    }

}
